<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','weibooauth','html','form'));
		$this->load->model('usermanager');
		$this->load->model('project');
		$this->load->library(array('form_validation','input'));
	}
	
	/**
	 * 
	 * 个人设置的显示页
	 */
	public function index($msg=null)
	{
		$this->load->view('/common/head', array('title' => '微笑网 | 个人设置'));
		
		if($this->weixiao->is_login())
		{
			$projects = $this->project->all();
			$options = array();
			foreach ($projects as $p) {
				$options[$p->id] = $p->name;
			}
			$data = array(
				'user' => $this->weixiao->get_cur_user(),
				//获得所有绑定的数据
				'bindings' => $this->usermanager->get_binding_by_uid($this->weixiao->get_cur_user()->id),
				'msg' => $msg,
				'projects' => $options,
			);
			$this->load->view('setting', $data);
		}
		else
		{
			echo '请先<a href="/">登录</a>';
		}
		
		$this->load->view('/common/footer', array('before_end_body' => ''));
	}
	
	/**
	 * 保存设置
	 */
	function save() {
//		$this->form_validation->set_rules('email', 'Email', 'required');
//		if ($this->form_validation->run() == FALSE)
		if (false)
		{
			$this->index();
		}
		else
		{
			$project = $this->input->get_post('project');
			$follow_weibo = $this->input->get_post('follow_weibo');
			$user = $this->weixiao->get_cur_user();
			if (!empty($user))
			{
				$values = array(
					'selected_project_id' => $project+0,
					'follow_weibo' => $follow_weibo,
				);
				$cur_user = $this->usermanager->update_user($user->id, $values);
				$this->index(var_dump($values));
			}
		}
		
	}
}
?>